/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  12:09:59 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : MmMatmasInbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class MmMatmasInbServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(MmMatmasInbServices.class);

	public MmMatmasInbServices(EntityManager em) {
		super(em);
	}
	
	public MmMatmasInb findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "MmMatmasInb.findById"));				
	}
	
	public List<MmMatmasInb> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "MmMatmasInb.findByInstanceNo"));
	}
	
	public List<MmMatmasInb> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmMatmasInb.findAllIsSync", mapWhereValue));				
	}
	
	public MmMatmasInb UpdatedMmMatmasInbIsSyncYes(MmMatmasInb mmMatmasInb) throws Exception {
		if(mmMatmasInb != null)
		{		
			mmMatmasInb.setIssync("Y");
			return (MmMatmasInb) super.updated(mmMatmasInb);
		}else
			return null;
	}
	
	public MmMatmasInb Updated(MmMatmasInb mmMatmasInb) throws Exception {
		if(mmMatmasInb != null)
		{		
			return (MmMatmasInb) super.updated(mmMatmasInb);
		}else
			return null;
	}

	public MmMatmasInb InsertedOrUpdated(MmMatmasInb vlaue) throws Exception {
		MmMatmasInb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("MM_MATMAS_INB");					
				obj.setPromisStagingId(id);
				obj.setCreatedBy(this.getUser());
				obj.setUpdatedBy(this.getUser());				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public List<MmMatmasInb> Inserted(List<MmMatmasInb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmMatmasInb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("MM_MATMAS_INB");					
					obj.setPromisStagingId(id);
					obj.setCreatedBy(this.getUser());
					obj.setUpdatedBy(this.getUser());				
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}	
			}
			
			if(common_lst != null && CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<MmMatmasInb> Updated(List<MmMatmasInb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmMatmasInb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	

}
